from python_ai.common.xcommon import sep
import tensorflow as tf
import numpy as np
import sys
import os

# ATTENTION
# AttributeError: Tensor.graph is meaningless when eager execution is enabled.
tf.compat.v1.disable_eager_execution()

sep('init')
m, n = 10, 4
mat = tf.Variable(np.zeros((m, n)), dtype=tf.float64, name='mat')
tens = 10 ** np.arange(n)
tens[0] = 0
for_cols = tf.Variable(tens, dtype=tf.float64, name='for_cols')
series = np.arange(m) + 1
for_rows = tf.Variable(series, dtype=tf.float64, name='for_rows')
print(f'mat: {mat}')
print(f'for_col: {for_cols}')
print(f'for_row: {for_rows}')

sep('the calc gram')
sum01 = tf.add(mat, for_cols, name='sum01')
print(f'sum01: {sum01}')
sum01_T = tf.transpose(sum01, name='sum01_T')
print(f'sum01_T: {sum01_T}')
sum02_T = tf.add(sum01_T, for_rows, name='sum02_T')
print(f'sum02_T: {sum02_T}')
sum02 = tf.transpose(sum02_T, name='sum02')
print(f'sum02:{sum02}')
sum02_int = tf.cast(sum02, dtype=tf.int64, name='sum02_int')
print(f'sum02_int: {sum02_int}')

sep('the calc')
with tf.compat.v1.Session() as sess:
    sess.run(tf.compat.v1.global_variables_initializer())
    res = sess.run(sum02_int)
    print(res)
    dirpath = './logs.tmp.d/' + os.path.basename(sys.argv[0])
    print(dirpath)
    with tf.compat.v1.summary.FileWriter(dirpath,
                                         sess.graph):
        pass
